package beautiful.butterfly.drds.data_exchange.report;


import beautiful.butterfly.drds.data_exchange.constant.Constants;
import beautiful.butterfly.drds.data_exchange.constant.State;
import beautiful.butterfly.drds.data_exchange.util.Configuration;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Collector
{
    private Map<Integer, Message> taskIdToMessageMap = new ConcurrentHashMap<Integer, Message>();


    @Setter
    @Getter
    private Long jobId;

    public Collector(Long jobId)
    {
        this.jobId = jobId;
    }

    public Map<Integer, Message> getTaskIdToMessageMap()
    {
        return taskIdToMessageMap;
    }


    public void registerTGCommunication(List<Configuration> taskGroupConfigurationList)
    {
        for (Configuration configuration : taskGroupConfigurationList)
        {
            int taskGroupId = configuration.getInt(
                    Constants.container_taskgroup_id);
            TaskGroupIdToMessageMap.put(taskGroupId, new Message());
        }
    }

    public void registerTaskCommunication(List<Configuration> configurationList)
    {
        for (Configuration configuration : configurationList)
        {
            int taskId = configuration.getInt(Constants.task_id);
            this.taskIdToMessageMap.put(taskId, new Message());
        }
    }

    public Message collectFromTask()
    {
        Message message = new Message();
        message.setState(State.succeeded);

        for (Message taskMessage :
                this.taskIdToMessageMap.values())
        {
            message.merge(taskMessage);
        }

        return message;
    }

    public Message collectFromTaskGroup()
    {
        return TaskGroupIdToMessageMap.getMessage();
    }

    public Map<Integer, Message> getTaskGroupIdToMessageMap()
    {
        return TaskGroupIdToMessageMap.getTaskGroupIdToMessageMap();
    }

    public Message getMessage(Integer taskGroupId)
    {
        return TaskGroupIdToMessageMap.getMessage(taskGroupId);
    }

    public Message getMessage2(Integer taskId)
    {
        return this.taskIdToMessageMap.get(taskId);
    }
}
